<iframe id="voteuploadframe" name="voteuploadframe" width="0" height="0" marginwidth="0" frameborder="0" src="about:blank"></iframe>
<!--文字投票 begin-->
<!--{if 'word' == $tab}-->
<div id="vote_publish_wp"> 
<form method="post" autocomplete="off" enctype="multipart/form-data" id="FORM" name="FORM">
<input type="hidden" name="tab" value="word" id="tab"/>
<!--{if $this->Get['arf'] == 'edit'}-->
<input type="hidden" name="vid" value="{$vid}"/>
<!--{/if}-->
<p class="index_v">
<sub><span>*</span>投票标题：</sub>
<sup><input id="subject" name="subject" value="{$vote[subject]}" type="text"></sup>
<sup id="subject_tips" style="display:none; color:#ff0000; display:block;"></sup>
</p>	

<p class="vote_addpictext">
<sub>&nbsp;</sub>
<sup><span class="lv_icon1"></span><a id="addtip" href="javascript:void(0);">添加投票说明</a></sup>
</p>

<p class="vote_addpictext" id="intropoll" style="display:none">
<sub>&nbsp;</sub>
<sup><input type="file" name="image" id="voteimg"><br>(宽度小于600px，大小不超过1M)<br><textarea id="message" name="message" class="vt_textarea">{$vote[message]}</textarea></sup>
</p>

<p class="vote_select">
<sub><span>*</span>投票选项：</sub>
<sup><span>可设置20项,每项最多20个汉字</span></sup>
</p>

<div id="option_wp">
	<!--{loop $options $value}-->
		<!--{eval $key=$value-1;}-->
		<!--{eval $readonly="";if (isset($opts[$key][pid]) && $is_voted && MEMBER_ROLE_TYPE != 'admin')$readonly='readonly="readonly"';}-->
		<!--{eval $opt_name="option[]";if (isset($opts[$key][pid]))$opt_name="old_option[".$opts[$key][oid]."]";}-->
		<p>
		<sub>&nbsp;</sub>
		<sup class="vote_al">{$value}.</sup>
		<sup class="vote_ar"><input id="option_{$value}" name="{$opt_name}" value="{$opts[$key][option]}" type="text" {$readonly} ></sup>
		<sup id="option_{$value}_tips" style="display:none;"></sup>
		</p>
	<!--{/loop}-->
</div>

<p id="moretip">
<sub>&nbsp;</sub>
<sup><span class="vote_addicon"></span><a href="javascript:;" onclick="showMoreOption({$perpage},{$max_option});" onfocus="this.blur();">增加选项</a></sup>
</p>

<!--{if !$is_voted}-->
<p>
<sub>单选/多选：</sub>
<sup class="input">
	<select name="maxchoice" id="maxchoice">
	<option value="1">单选</option>
	<!--{loop $maxchoice $i}-->
	<!--{eval if($i==1) continue;}-->
	<!--{eval $selected='';if($i==$vote['maxchoice']) $selected='selected="selected"';}-->
	<option value="{$i}" {$selected}>最多可选{$i}项</option>
	<!--{/loop}-->
	</select>
</sup>
</p>
<!--{/if}-->

<p>
<sub>投票结果：</sub>
<sup>
<!--{eval if(empty($checked['is_view'])) $checked['is_view'][1]="checked"}-->
<input type="radio" id='is_view_1' name="is_view"   value="1" {$checked[is_view][1]} class="vt_radio1" defaultvalue="1"/><label for="is_view_1">任何人可见</label>
<input type="radio"  id="is_view_2" name="is_view"  value="0" {$checked[is_view][0]} class="vt_radio1" /><label for="is_view_2">投票后可见</label>
</sup>
</p>

<p>
<sub>截止时间：</sub>
<sup class="vote_endtime input" style="float:left; display:inline">
<input id="expiration" name="expiration" value="{$expiration}" onclick="WdatePicker({minDate:'%y-%M-%d'})"  type="text" readonly="readonly">
&nbsp;&nbsp;
{$hour_select}&nbsp;时&nbsp;&nbsp;{$min_select}&nbsp;分
</sup>
</p>

<p>
<sub>&nbsp;</sub>
<sup>
<input type="button" class="btnText" id="vote_save_btn" value="提 交" />
<!--{if $this->Get['arf'] == "edit"}-->
	&nbsp;&nbsp;<input type="button" class="canlText" id="vote_cancel_btn" value="取 消" />
<!--{/if}-->
<span style="margin-left:5px;" id="vote_submit_tips"></span>
</sup>
</p>

</form>
</div>
<!--文字投票 end-->

<!--图片投票 begin-->
<!--{elseif 'pic' == $tab}-->
<div id="vote_publish_wp"> 
  <form method="post" action="#" enctype="multipart/form-data" target="voteuploadframe" name="FORM" id="FORM">
    <input type="hidden" name="tab" value="pic" id="tab"/>
    <!--{if $this->Get['arf'] == 'edit'}-->
    <input type="hidden" name="vid" value="{$vid}"/>
	<!--{/if}-->
	<p class="index_v">
	  <sub><span>*</span>投票标题：</sub>
	  <sup><input id="subject" name="subject" value="{$vote[subject]}" type="text"></sup>
	  <sup id="subject_tips" style="display:none; color:#ff0000; display:block;"></sup>
	</p>	

	<p class="vote_addpictext">
	  <sub>&nbsp;</sub>
	  <sup><span class="lv_icon1"></span><a id="addtip" href="javascript:void(0);">添加投票说明</a></sup>
	</p>

	<p class="vote_addpictext" id="intropoll" style="display:none">
	  <sub>&nbsp;</sub>
	  <sup><input type="file" name="image" id="voteimg"><br>(宽度小于600px，大小不超过1M)<br><textarea id="message" name="message" class="vt_textarea">{$vote[message]}</textarea></sup>
	</p>

	<p class="vote_select">
	  <sub><span>*</span>投票选项：</sub>
	  <sup><span>可设置20项,每项最多20个汉字</span></sup>
	</p>

	<div id="option_wp">
	<!--{loop $options $value}-->
		<!--{eval $key=$value-1;}-->
		<!--{eval $readonly="";if (isset($opts[$key][pid]) && $is_voted && MEMBER_ROLE_TYPE != 'admin')$readonly='readonly="readonly"';}-->
		<!--{eval $opt_name="option[]";$pic_name = "pic_id[]"; if (isset($opts[$key][pid])) $opt_name="old_option[".$opts[$key][oid]."]";}-->
		<!--{eval $pic_name = "pic_id[]";if (isset($opts[$key][pid])) $pic_name="old_pic[".$opts[$key][oid]."]";}-->
		<dl class="pic_vote">
        <dt>
        <span class="v_num">{$value}.</span>
        <div class="upbtn">        
        <s><input name="pic{$value}" type="file" id="{$value}" class="vote_file"></s>
        <a class="upic add" href="javascript:;"><span>传图片</span></a>  
         
        <a class="upic reAdd" href="javascript:;" >
        <img src='{$opts[$key][picurl]}' id="pic_show_{$value}" />
        <span class="reAddspan"><em><i>重新上传</i></em></span>
        </a>   
        <input type="hidden" name="{$pic_name}" id=pic_id_{$value} value="{$opts[$key][pid]}">

        </div>
		</dt>
        
        <dd>
		<input id="option_{$value}" name="{$opt_name}" value="{$opts[$key][option]}" type="text" {$readonly} >
		<sup id="option_{$value}_tips" style="display:none;"></sup>
        </dd>{if !$this->Get['arf'] == 'edit'}<a href="#" onclick="$(this).parent().remove();return false;" style="float:right;">删除</a>{/if}
        <div class="dl_line"></div>
		
		</dl>
	<!--{/loop}-->
	</div>

	<p id="moretip">
	  <sub>&nbsp;</sub>
	  <sup><span class="vote_addicon"></span><a href="javascript:;" onclick="showMoreOption({$perpage},{$max_option});" onfocus="this.blur();">增加选项</a></sup>
	</p>

	<!--{if !$is_voted}-->
	<p>
	  <sub>单选/多选：</sub>
	  <sup class="input">
	    <select name="maxchoice" id="maxchoice">
	      <option value="1">单选</option>
		  <!--{loop $maxchoice $i}-->
		  <!--{eval if($i==1) continue;}-->
		  <!--{eval $selected='';if($i==$vote['maxchoice']) $selected='selected="selected"';}-->
		  <option value="{$i}" {$selected}>最多可选{$i}项</option>
		  <!--{/loop}-->
		</select>
	  </sup>
	</p>
	<!--{/if}-->

	<p>
	<sub>投票结果：</sub>
	  <sup>
		<!--{eval if(empty($checked['is_view'])) $checked['is_view'][1]="checked"}-->
		<input type="radio" id='is_view_1' name="is_view"   value="1" {$checked[is_view][1]} class="vt_radio1" defaultvalue="1"/><label for="is_view_1">任何人可见</label>
		<input type="radio"  id="is_view_2" name="is_view"  value="0" {$checked[is_view][0]} class="vt_radio1" /><label for="is_view_2">投票后可见</label>
	  </sup>
	</p>

	<p>
	  <sub>截止时间：</sub>
	  <sup class="vote_endtime input" style="float:left; display:inline">
		<input id="expiration" name="expiration" value="{$expiration}" onclick="WdatePicker({minDate:'%y-%M-%d'})"  type="text" readonly="readonly">
		&nbsp;&nbsp;
		{$hour_select}&nbsp;时&nbsp;&nbsp;{$min_select}&nbsp;分
	  </sup>
	</p>

	<p>
	  <sub>&nbsp;</sub>
	  <sup>
		<input type="button" class="btnText" id="vote_save_btn" value="提 交" />
		<!--{if $this->Get['arf'] == "edit"}-->
			&nbsp;&nbsp;
			<input type="button" class="canlText" id="vote_cancel_btn" value="取 消" />
		<!--{/if}-->
		<span style="margin-left:5px;" id="vote_submit_tips"></span>
	  </sup>
	</p>

  </form>
</div>
<!--{/if}-->
<!--图片投票 end-->

<!--JS开始-->
<script language="javascript">

	
	function initIntro()
	{
		if($('#intropoll').is(':hidden')) {
			$('#intropoll').show();
			$('#addtip').html("隐藏投票详细说明");
		} else {
			if (($('#message').val().length == 0) || (confirm("详细说明将被清空，你确定要隐藏吗？"))) {
				$('#intropoll').hide();
				$('#message').val('');
				$('#addtip').html("添加投票详细说明");
			}
		}
	}
	
	function showMoreOption(perpage, max_option)
	{
		if($("#option_wp").find("dl").length){
			var iRowNums = $("#option_wp").find("dl").length;
		}else {
			var iRowNums = $("#option_wp").find("p").length;
		}
		if (iRowNums >= max_option) {
			return ;
		}
		if ((iRowNums + perpage) >= max_option) {
			$("#moretip").hide();
		}
		var tab = $('#tab').val();
		for (i=1;i<=perpage;i++) {
			var num = iRowNums + i;
			if(tab == 'pic'){
				$("#option_wp").append('<dl class="pic_vote"><dt><span class="v_num">'+num+'</span><div class="upbtn"><s><input name="pic'+num+'" type="file" id="'+num+'" class="vote_file" onchange="onLoadPic(this.id);"></s><a class="upic add" href="javascript:;"><span>传图片</span></a><a class="upic reAdd" href="javascript:;" ><img src="images/none.png" id="pic_show_'+num+'" /><span class="reAddspan"><em><i>重新上传</i></em></span></a><input type="hidden" name="pic_id[]" id=pic_id_'+num+' value=""></div></dt><dd><input id="option_'+num+'" name="option[]" value="" type="text"><sup id="option_'+num+'_tips" style="display:none;"></sup></dd><div class="dl_line"></div></dl>');
			}else {
				$("#option_wp").append('<p><sub>&nbsp;</sub><sup class="vote_al">'+num+'.</sup><sup class="vote_ar"><input id="option_'+num+'" name="option[]" value="" type="text" onblur="setVoteOptionBlur(this);"></sup><sup id="option_'+num+'_tips" style="display:none;"></sup></p>');
			}
		}
	}
	
	function checkVoteSubject()
	{
		var subject = $("#subject").val();
		if (strlen(subject) < 2) {
			$("#subject_tips").html("标题必须有哦，最多25个汉字");
			$("#subject_tips").show();
			
			return false;
		} else {
			$("#subject_tips").html("<img src='images/accept.png' class='vote_accept'/>");
			$("#subject_tips").show();
		}
		return true;
	}
	
	function checkVoteOption(obj)
	{
		var opt_tips_id = '#'+obj.id+ "_tips";
		if (strlen(obj.value) > 0) {
			$(opt_tips_id).html("<img src='images/accept.png' class='vote_accept'/>");
			$(opt_tips_id).show();
		} else {
			$(opt_tips_id).hide();
		}
	}
	
	//获取有效的option数
	function validOptionNumber()
	{
		var obj_options = $("#option_wp :text");
		var len = obj_options.length;
		var count = 0;
		for (i=0;i<len;++i) {
			if (strlen(obj_options[i].value) > 0) {
				count++;
			}
		}
		return count;
	}
	
	//表单数据验证
	function validate() 
	{
		if (checkVoteSubject() == false) {
			$("#subject").focus();
			show_message("标题必须有哦");
			return false;
		}
		
		var count = validOptionNumber();
		if (count < 2) {
			show_message('选项太少啦，至少2个哦');
			return false;
		}
		//验证没个上传的图片是否有对应的值
		if($(".upbtn").length>0){
			for(var i=1;i<=$(".upbtn").length;i++){
				if($("#pic_id_"+i).val() || $("#option_"+i).val()){
					if(!$("#pic_id_"+i).val() || !$("#option_"+i).val()){
						if(!$("#option_"+i).val()){
							show_message("第"+i+"个选项没有填哦");
						}else{
							show_message("第"+i+"个选项没有上传图片");
						}
						return false;
					}
				}
			}
		}
		return true;
	}
		
	//发起投票
	function publishVote(type)
	{
		if (isUndefined(type)) {
			var type = 'create';
		}
		if (validate()) {
			$("#vote_submit_tips").html("<span class='loading'>提交中，请稍候……</span>");
			$("#vote_save_btn").attr('disabled', true);
			
			//对发布时间的处理
			var expiration = $("#expiration").val();
			var old = expiration;
			var hour = $("#hour").val();
			var min = $("#min").val();
			if (!isUndefined(hour) && !isUndefined(min)) {
				expiration += ' '+hour+":"+min;
			}
			$("#expiration").val(expiration);
			var post_data = $('#FORM').serialize();
			$("#expiration").val(old);
			$.post(
				"ajax.php?mod=vote&code="+type+"&rand="+Math.random()+"&item="+__APPITEM__+"&item_id="+__APPITEM_ID__, 
				post_data, 
				function(r){
					publishVoteCallBack(r);
				},
				'json'
			);
		}
	}
	
	<!--{if $this->Get['arf'] == 'index'}-->
		//首页调用
		function publishVoteCallBack(r)
		{
			$("#vote_save_btn").attr('disabled', false);
			$("#vote_submit_tips").html("");
			if (r.done) {
				loadpic(r.retval.vid);
				MessageBox('notice', r.msg);
				if($('#i_already').length > 0){
					$('#i_already').val(r.retval.content);
				}else{
					$('#i_alreadyajax').val(r.retval.content);
				}
				//$('#mapp_item').val(r.retval.item);
				//$('#mapp_item_id').val(r.retval.vid);
				$(".menu_vsb").hide();
			} else {
				MessageBox('warning', r.msg);
			}
		}
	<!--{elseif $this->Get['arf'] == 'edit'}-->
		function publishVoteCallBack(r)
		{
			$("#vote_save_btn").attr('disabled', false);
			if (r.done) {
				loadpic(r.retval);
				location.reload();
			} else {
				$("#vote_submit_tips").html("");
				MessageBox('warning', r.msg);
			}
		}
	<!--{else}-->
		function publishVoteCallBack(r)
		{
			$("#vote_save_btn").attr('disabled', false);
			$("#vote_submit_tips").html("");
			if (r.done) {
				loadpic(r.retval.vid);
				$("#vote_publish_tips").html(r.msg);
				//$('#item').val('vote');
				//$('#item_id').val(r.retval.vid);
				$('#topic_simple_content').html(r.retval.content);
				var handle_key = 'vote_success_share';
				showDialog(handle_key, 'local', '分享到'+__P_WEIBO__, {id:'vote_publish_ret_wp'}, 500);
				$('#topic_simple_close_btn').click(
					function() {
						closeDialog(handle_key);
						location.href = "index.php?mod=vote&code=view&vid="+r.retval.vid;
					}
				);
				$('#topic_simple_share_btn').click(
					function () {
						var response = function() {
							location.href = "index.php?mod=vote&code=view&vid="+r.retval.vid;
						}
						//publishSimpleTopic($('#topic_simple_content').val(), 'vote', r.retval.vid, {response:response});
						publishSimpleTopic($('#topic_simple_content').val(), '', 0, {response:response});
					}
				);
			} else {
				MessageBox('warning', r.msg);
			}
		}
	<!--{/if}-->
	
	function setVoteOptionBlur(obj)
	{
		checkVoteOption(obj);
		
		//select选择项设置
		//得到最大的index
		var max_index = $("#maxchoice option").length;
		var count = validOptionNumber();
		if (count > max_index) {
			if (count >= 2) {
				var val = max_index + 1;
				$("#maxchoice").append("<option value='"+val+"'>最多可选"+val+"项</option>");
			}
		} else if (count < max_index) {
			if (count >= 1) {
				$("#maxchoice option:last").remove(); 
			}
		} 
	}
	
	$(document).ready(function(){
		$("#addtip").click(
			function(){initIntro();}
		);
		//自动截取字符串
		$("#subject").keyup(
			function(){
				checkPublishText(25, 'subject');
				return false;
			}
		);
		
		//检查subject
		$("#subject").blur(
			function(){
				checkVoteSubject();
			}
		);
		
		$("#option_wp input").focus(
			function() {
				checkVoteSubject();
			}
		);
		
		$("#option_wp input").keyup(
			function() {
				checkPublishText(20, this.id);
				return false;
			}
		);
		
		$("#option_wp input").blur(
			function () {
				setVoteOptionBlur(this);
			}
		);
		
		<!--{if $this->Get['arf'] == 'edit'}-->
			//initIntro();
			$("#vote_save_btn").click(function(){publishVote('edit');});
			$("#vote_cancel_btn").click(function(){closeDialog("{$this->Get['handle_key']}");});
		<!--{else}-->
			$("#vote_save_btn").click(function(){publishVote();});
		<!--{/if}-->

		$(".vote_file").bind('change',function(){
			var id = this.id;
			onLoadPic(id);
		});
	});
	function onLoadPic(id){
		var pid = $('#pic_id_'+id).val();
		var fn = $('#'+id).val();
		if (!/.(gif|jpg|png|jpeg)$/i.test(fn)) {
			alert("请上传jpg、jpeg、gif、png格式的图片。");
			return false;
		}
		//document.getElementById('uploading').style.display = 'block';
		document.FORM.target="voteuploadframe";
		document.FORM.action="ajax.php?mod=vote&code=onLoadPic&id="+id+"&pid="+pid;
		$('#FORM').submit();
	}
	function loadpic(vid){
		var fn = $('#voteimg').val();
		if(fn){
			if (!/.(gif|jpg|png|jpeg)$/i.test(fn)) {
				alert("图片没有上传，只接受上传jpg、jpeg、gif、png格式的图片。");
				return false;
			}
			document.FORM.target="voteuploadframe";
			document.FORM.action="ajax.php?mod=vote&code=loadpic&vid="+vid;
			$('#FORM').submit();
		}
	}
</script>
<script src="js/date/WdatePicker.js" type="text/javascript"></script>